<script type="text/javascript">
	var setting = {
		data: {
			key: {
				title: "title"
			},
			simpleData: {
				enable: true
			}
		},
		callback: {
			onCheck:      onCheck,
			onRemove:     onRemove
		},
		edit:{
			enable:        true,
			showRemoveBtn: showRemoveBtn,
			removeTitle: "remove"
		}
	};

	var zNodes =[
	    <volist name="node" id="v">
		 {id:<php>echo $v['id'];</php>, pId:<php>echo $v['pid'];</php>, name:"<php>echo $v['title'];</php>", url:"__URL__/sort/id/<php>echo $v['id'];</php>/restid/<php>echo $v['restid'];</php>",target:"_self"},
		</volist>
	];
	
	function getSelectedNode(treeDemo){
		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		return treeObj.getCheckedNodes(true);
	}

	function onCheck(e, treeId, treeNode) {
		count();
	}

	function setTitle(node) {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = node ? [node]:zTree.transformToArray(zTree.getNodes());
		for (var i=0, l=nodes.length; i<l; i++) {
			var n = nodes[i];
			n.title = "[" + n.id + "] isFirstNode = " + n.isFirstNode + ", isLastNode = " + n.isLastNode;
			zTree.updateNode(n);
		}
	}
	function count() {
		function isForceHidden(node) {
			if (!node.parentTId) return false;
			var p = node.getParentNode();
			return !!p.isHidden ? true : isForceHidden(p);
		}
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		checkCount = zTree.getCheckedNodes(true).length,
		nocheckCount = zTree.getCheckedNodes(false).length,
		hiddenNodes = zTree.getNodesByParam("isHidden", true),
		hiddenCount = hiddenNodes.length;

		for (var i=0, j=hiddenNodes.length; i<j; i++) {
			var n = hiddenNodes[i];
			if (isForceHidden(n)) {
				hiddenCount -= 1;
			} else if (n.isParent) {
				hiddenCount += zTree.transformToArray(n.children).length;
			}
		}

		$("#isHiddenCount").text(hiddenNodes.length);
		$("#hiddenCount").text(hiddenCount);
		$("#checkCount").text(checkCount);
		$("#nocheckCount").text(nocheckCount);
	}
	function showNodes() {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getNodesByParam("isHidden", true);
		zTree.showNodes(nodes);
		setTitle();
		count();
	}
	function hideNodes() {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getSelectedNodes();
		if (nodes.length == 0) {
			alert("请至少选择一个节点");
			return;
		}
		zTree.hideNodes(nodes);
		setTitle();
		count();
	}
	
	function onRemove(e, treeId, treeNode) {
		showLog("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
	}
	
	function showRemoveBtn(treeId, treeNode) {
		return !treeNode.isFirstNode;
	}

	$(document).ready(function(){
		$.fn.zTree.init($("#treeDemo"), setting, zNodes);
		$("#hideNodesBtn").bind("click", {type:"rename"}, hideNodes);
		$("#showNodesBtn").bind("click", {type:"icon"}, showNodes);
		setTitle();
		count();
	});

</script>